import { _decorator, Component, Button, Prefab, instantiate, Node } from 'cc';
import { Game } from '../core/Game';
import { Databus } from '../core/Databus';
import DataEventReport from '../platform/DataEventReport';
import { BottomBannerAd } from '../platform/ad/BottomBannerAd';
import { uniConfig } from '../config/UniConfig';
import { updateSetting } from '../services/Setting';
import { RewardedVideoAd } from '../platform/ad/RewardedVideoAd';
import { UniformFunction } from '../platform/UniformFunction';
import { WxSdkManager } from '../platform/ad/WxSdkManager';
const { ccclass, property } = _decorator;

const databus = Databus.Instance();
@ccclass('SelectPage')
export class SelectPage extends Component {
  // 激励广告弹窗
  @property(Prefab)
  RewordVideoAds: Prefab = null;
  private rewordVideoInstance: Node = null;

  private backButton: Button = null;
  private eightBallButton: Button = null;
  private nineBallButton: Button = null;
  private snookerBallButton: Button = null;
  private unlockGame = "";
  onLoad(): void {
    this.eightBallButton = this.node.getChildByName('EightBallButton').getComponent(Button);
    this.nineBallButton = this.node.getChildByName('NineBallButton').getComponent(Button);
    this.snookerBallButton = this.node.getChildByName('SnookerBallButton').getComponent(Button);
    this.backButton = this.node.getChildByName('BackButton').getComponent(Button);
    
    this.eightBallButton.node.on(Button.EventType.CLICK, this.startEightBallGame, this);
    this.nineBallButton.node.on(Button.EventType.CLICK, this.startNineBallGame, this);
    this.snookerBallButton.node.on(Button.EventType.CLICK, this.startSnookerBallGame, this);
    this.backButton.node.on(Button.EventType.CLICK, this.backPage, this);
    BottomBannerAd.Instance().create();
  }
  public start() {
    RewardedVideoAd.Instance().create();
    if (this.checkAndSetGoldCoin()) {
      BottomBannerAd.Instance().show();
    }
  }



  protected onEnable(): void {
   // console.log("555555555555555")
    WxSdkManager.toggleCustomCol33Ad(true)
  }

  protected onDisable(): void {
  //  console.log("777777777777777777")
    WxSdkManager.toggleCustomCol33Ad(false)
  }


  update(deltaTime: number) {
    if (databus.GameLockConfig.onNineGame === 1) {
      this.node.getChildByName('NineBallButton').getChildByName('LockSprite').active = false;
    }

    if (databus.GameLockConfig.onSnookerGame === 1) {
      this.node.getChildByName('SnookerBallButton').getChildByName('LockSprite').active = false;
    }

    if (databus.GameLockConfig.onyouyouGame === 1) {
      this.node.getChildByName('EightBallButton').getChildByName('LockSprite').active = false;
    }


    

  }
  //这个是8球的
  startEightBallGame(button: Button) {
    Game.Instance().initEightBall();



    if (!databus.GameLockConfig.onyouyouGame) {
      this.unlockGame = "youyouyou";
      this.checkGameLock();
      return;
    }



    if (!this.checkAndSetGoldCoin()) return;
    DataEventReport.Instance().reportComponentEvent(databus.sceneStack.currentPageName(), 'EightBallGame');
    databus.sceneStack.toMatchPage();
  }

  //这个是9球的
  startNineBallGame(button: Button) {
    Game.Instance().initNineBall();
    if (!databus.GameLockConfig.onNineGame) {
      this.unlockGame = "nine";
      this.checkGameLock();
      return;
    }
    
    if (!this.checkAndSetGoldCoin()) return;
    DataEventReport.Instance().reportComponentEvent(databus.sceneStack.currentPageName(), 'NineBallGame');
    databus.sceneStack.toMatchPage();
  }

  //这个是斯诺克
  startSnookerBallGame(button: Button) {
    Game.Instance().initSnookerBall();
    if (!databus.GameLockConfig.onSnookerGame) {
      this.unlockGame = "snooker";
      this.checkGameLock();
      return;
    }
    if (!this.checkAndSetGoldCoin()) return;
    DataEventReport.Instance().reportComponentEvent(databus.sceneStack.currentPageName(), 'SnookerBallGame');
    databus.sceneStack.toMatchPage();
  }

  public backPage(button: Button) {
    DataEventReport.Instance().reportComponentEvent(databus.sceneStack.currentPageName(), 'ReturnBack');
    BottomBannerAd.Instance().hide();
    databus.sceneStack.backScene();
  }
  public checkAndSetGoldCoin() {
    
    if (Game.Instance().battleCate.isFrientBattle()) {
      return true;
    }
    if (this.checkGoldCoin()) {
      return true;
    }
    databus.rad.adType = 0;
    this.rewordVideoInstance = instantiate(this.RewordVideoAds);
    this.rewordVideoInstance.setParent(this.node);
    this.rewordVideoInstance.active = true;
    return false;
  }

  public checkGameLock() {
    RewardedVideoAd.Instance().setCallback(this, this.watchVideoSuss, this.watchVideoFail);
    RewardedVideoAd.Instance().show();
    return false;
  }
  public watchVideoSuss() {
    if (this.unlockGame === "nine") {
      databus.GameLockConfig.onNineGame = 1;
      this.updateLockSetting();
      new UniformFunction().showInfoToast('解锁成功', 3000);
    }
    if (this.unlockGame === "snooker") {
      databus.GameLockConfig.onSnookerGame = 1;
      this.updateLockSetting();
      new UniformFunction().showInfoToast('解锁成功', 3000);
    }


    if(this.unlockGame == "youyouyou"){
      databus.GameLockConfig.onyouyouGame = 1;
      console.log('8球解锁成功了啊。。。。。。')
      this.updateLockSetting();
      new UniformFunction().showInfoToast('解锁成功', 3000);
    }

  }
  public watchVideoFail() {
    new UniformFunction().showInfoToast('解锁失败', 3000);
  }
  private checkGoldCoin() {
    return databus.selfInfo.goldCoin >= uniConfig.TicketCoinValue;
  }
  public updateLockSetting() {
    updateSetting(databus.selfInfo.userId, databus.selfInfo.userId, 6, 'gameLock', JSON.stringify(databus.GameLockConfig));
  }
}